package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "01. 相册业务管理模块")
@Slf4j
@RestController
@Validated
@RequestMapping("/albums")
public class AlbumController {

    @Autowired
    IAlbumService service;

    //http://localhost:9080/albums/add-new?name=TestName&description=TestDescription&sort=23
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    @PostMapping(value = {"/add-new"})
    public JsonResult<Void> addNew(@Validated AlbumAddNewDTO albumAddNewDTO){
        log.debug("开始处理添加相册的请求，参数：{}",albumAddNewDTO);
        service.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9080/albums/123/delete
    @ApiOperation("删除相册")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "long")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除的请求，参数：{}",id);
        service.delete(id);
        return JsonResult.ok();
    }

    //http://localhost:9080/albums/123/update
    @ApiOperation("修改相册数据")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id,AlbumUpdateDTO albumUpdateDTO){
        log.debug("开始处理修改相册的请求，参数：id={},albumUpdateDTO={}",id,albumUpdateDTO);
        service.updateById(id,albumUpdateDTO);
        return JsonResult.ok();
    }

    //http://localhost:9080/albums/3/info
    @ApiOperation("查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "long")
    @GetMapping("/{id:[0-9]+}/info")
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理查询相册的请求，参数：{}",id);
        return JsonResult.ok(service.getStandardById(id));
    }

    //http://localhost:9080/albums
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理查询相册列表的请求");
        List<AlbumListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @Deprecated
    @ApiOperation("测试正则删除相册优先级")
    @ApiOperationSupport(order = 201)
    @GetMapping("1/delete")
    public JsonResult<Void> deleteTest(){
        log.debug("测试正则的优先级");
        return JsonResult.ok();
    }

    @ApiOperation("测试删除相册")
    @ApiOperationSupport(order = 202)
    @ApiImplicitParam(name = "id",value = "相册id",paramType = "query",example = "-1")
    @GetMapping("delete")
    public JsonResult<Void> deleteTest2(@Range(min = 1,message = "测试删除相册失败，id值必须大于0或更大的有效整数") Long id){
        log.debug("执行deleteTest2");
        return JsonResult.ok();
    }
}
